﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ZDBase
{
    public partial class FileTab : UserControl
    {
        public OpenFileDialog _dlg;
        DataTable dataT;
        public bool mod = false;
        public FileTab()
        {
            InitializeComponent();
            Load += new EventHandler(FileTab_Load);

        }

        void FileTab_Load(object sender, EventArgs e)
        {
            dataT = new DataTable(_dlg.SafeFileName);
            dataT.Columns.Add("1", typeof(int));
            dataT.Columns.Add("2", typeof(string));
            bindingSource1.DataSource = dataT;
            bindingNavigator1.BindingSource = bindingSource1;
            DataViewTable.DataSource = bindingSource1;
            dataT.ColumnChanged += new DataColumnChangeEventHandler(dataT_ColumnChanged);
            dataT.RowChanged += new DataRowChangeEventHandler(dataT_RowChanged);
            dataT.RowDeleted += new DataRowChangeEventHandler(dataT_RowChanged);
            dataT.TableNewRow += new DataTableNewRowEventHandler(dataT_TableNewRow);
            dataT.TableCleared += new DataTableClearEventHandler(dataT_TableCleared);
            dataT.Columns.CollectionChanged += new CollectionChangeEventHandler(Columns_CollectionChanged);

        }

        void Columns_CollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            mod = true;
        }

        void dataT_TableCleared(object sender, DataTableClearEventArgs e)
        {
            mod = true;
        }

        void dataT_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            mod = true;
        }
        void dataT_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            mod = true;
        }

        void dataT_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            mod = true;

        }

        private void Edit_Click(object sender, EventArgs e)
        {
            ToolStripButton btn = (ToolStripButton)sender;
            if (btn.Checked)
            {
                EditMenu.Enabled = true;
            }
            else
            {
                EditMenu.Enabled = false;
            }
        }

        private void addColum_Click(object sender, EventArgs e)
        {
            ColumnAddControl ct = new ColumnAddControl();
            Form f = new Form();
            f.Controls.Add(ct);
            f.AutoSize = true;
            f.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            f.ClientSize = ct.ClientSize;
            DialogResult d = f.ShowDialog();
            if (d == DialogResult.OK)
            {
                Type tmp = Type.GetType("System." + ct.TypeColumn.Text);
                DataColumn curent = dataT.Columns.Add(ct.NameColumn.Text, tmp);
                if (tmp == typeof(string))
                {
                    curent.MaxLength = int.Parse(ct.maskedTextBox1.Text);
                }


            }

        }

        private void DelColumn_Click(object sender, EventArgs e)
        {
            DellColumns ct = new DellColumns();
            Form f = new Form();
            f.Controls.Add(ct);
            f.AutoSize = true;
            f.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            f.ClientSize = ct.ClientSize;
            for (int i = 0; i < dataT.Columns.Count; i++)
            {
                ct.ColumsName.Items.Add(dataT.Columns[i].ToString(), false);
            }

            DialogResult d = f.ShowDialog();
            if (d == DialogResult.OK)
            {
                for (int i = 0; i < ct.ColumsName.CheckedItems.Count; i++)
                {
                    dataT.Columns.Remove((string)ct.ColumsName.CheckedItems[i]);

                }


            }
        }

        internal void SaveFile(System.IO.Stream stream)
        {
            ///Structure
            ///4 byte -size header
            /// || -Spliter table
            /// after 2+size header dat
            /// 
            int size = 0;
            int blocksize = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dataT.Columns.Count; i++)
            {
                if (i == 0)
                {
                    sb.Append(dataT.Columns[i].ColumnName + "{" + dataT.Columns[i].DataType.Name + ":" + dataT.Columns[i].MaxLength + "}");
                }
                else
                {
                    sb.Append("||" + dataT.Columns[i].ColumnName + "{" + dataT.Columns[i].DataType.Name + ":" + dataT.Columns[i].MaxLength + "}");
                }

                if (dataT.Columns[i].MaxLength > 0)
                {
                    blocksize += dataT.Columns[i].MaxLength * 2;
                }
                else
                {
                    blocksize += dataT.Columns[i].DataType.StructLayoutAttribute.Size;

                }
               
            }
            sb.AppendLine();
            byte[] tmp = Encoding.ASCII.GetBytes(sb.ToString());
            size = tmp.Length;
            for (int i = 0; i < dataT.Rows.Count; i++)
            {
                string tmps = "";
                for (int c = 0; c < dataT.Columns.Count; c++)
                {
                    object dat = dataT.Rows[i][dataT.Columns[c].ColumnName];
                    if (c == 0)
                    {
                        tmps += dat;
                    }
                    else
                    {
                        tmps += "||" + dat;
                    }

                }
                sb.AppendLine(tmps);
            }

           using(
               StreamWriter sw = new StreamWriter(stream)           
            ){
            sw.Write(sb.ToString());
            }
        }
    }
}
